﻿using System;
using System.Collections.Generic;
using System.Text;
using rkShared.Serialization;
using rkShared.BEncode;
using System.Globalization;

namespace rkShared
{
    //3 boyutlu nokta ve vektörleri tanımlayan sınıf
    [Serializable]
    public class Vector3:IBinarySerializable
    {
        public float x=0.0f;
        public float y=0.0f;
        public float z=0.0f;
		
		public Vector3()
		{
			x = 0.0f;
			y = 0.0f;
			z = 0.0f;
		}
		public Vector3(Vector3 v)
		{
			x = v.x;
			y = v.y;
			z = v.z;
		}
		public Vector3(float xPos,float yPos,float zPos)
		{
			x = xPos;
			y = yPos;
			z = zPos;
		}
		
		public void Copy(Vector3 v)
		{
			x = v.x;
			y = v.y;
			z = v.z;
		}

        public float Magnitude()
        {
			return (float)Math.Sqrt(x*x+y*y+z*z);
        }
		//Vector3 sınıfı için - operatörü tanımla
		public static Vector3 operator -(Vector3 v1,Vector3 v2)
		{
			Vector3 temp = new Vector3();
			temp.x = v1.x - v2.x;
			temp.y = v1.y - v2.y;
			temp.z = v1.z - v2.z;
			return temp;
		}

		//Vector3 sınıfı için + operatörü tanımla
		public static Vector3 operator +(Vector3 v1,Vector3 v2)
		{
			Vector3 temp = new Vector3();
			temp.x = v1.x + v2.x;
			temp.y = v1.y + v2.y;
			temp.z = v1.z + v2.z;
			return temp;
		}

		//Vector3 float multiplication
		public static Vector3 operator *(Vector3 v1,float f)
		{
			Vector3 temp = new Vector3();
			temp.x = v1.x * f;
			temp.y = v1.y * f;
			temp.z = v1.z * f;
			return temp;
		}
		//Vector3 float division
		public static Vector3 operator /(Vector3 v1, float f)
		{
			Vector3 temp = new Vector3();
			temp.x = v1.x / f;
			temp.y = v1.y / f;
			temp.z = v1.z / f;
			return temp;
		}
		//Vector3 float addition
		public static Vector3 operator +(Vector3 v,float f)
		{
			Vector3 temp = new Vector3();
			temp.x = v.x + f;
			temp.y = v.y + f;
			temp.z = v.z + f;
			return temp;
		}
		//Vector3 float subtraction
		public static Vector3 operator -(Vector3 v,float f)
		{
			Vector3 temp = new Vector3();
			temp.x = v.x - f;
			temp.y = v.y - f;
			temp.z = v.z - f;
			return temp;
		}



        #region IBinarySerializable Members

        public void SerializeToDictionary(rkShared.BEncode.BEncodedDictionary dictionary)
        {
            dictionary.Add("x", x.ToString(CultureInfo.InvariantCulture));
            dictionary.Add("y", y.ToString(CultureInfo.InvariantCulture));
            dictionary.Add("z", z.ToString(CultureInfo.InvariantCulture));
        }

        public void DeserializeFromDictionary(rkShared.BEncode.BEncodedDictionary dict)
        {
            x = float.Parse(dict.GetString("x"), CultureInfo.InvariantCulture);
            y = float.Parse(dict.GetString("y"), CultureInfo.InvariantCulture);
            z = float.Parse(dict.GetString("z"), CultureInfo.InvariantCulture);
        }

        #endregion
    }
}
